package exam.axhu.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import exam.axhu.entity.Course;
import exam.axhu.entity.CourseInfo;
import exam.axhu.entity.Teacher;
import exam.axhu.service.CourseService;
import exam.axhu.service.TeacherService;


@SuppressWarnings("serial")
@Controller @Scope("prototype")
public class EditTeacher extends ActionSupport{
//defaultClass: teacherLogin
	@Resource TeacherService teacherService;
	@Resource CourseService courseService;
	
	private String teacherID = (String)ActionContext.getContext().getSession().get("teacherID");
	private Teacher teacher;
	private String searchInfo;
	public Teacher getTeacher() {
		return teacher;
	}
	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}
	public String getSearchInfo() {
		return searchInfo;
	}
	public void setSearchInfo(String searchInfo) {
		this.searchInfo = searchInfo;
	}
	
	
	public String editTeacherA(){
		ActionContext.getContext().put("editteacher", teacherService.find(teacher.getTeacherID()));
		return "Edit";
	}
	

	public String editTeacherB(){
		teacherService.update(teacher);
		addActionMessage("修改成功!欢迎!");
		ActionContext.getContext().put("teachers", teacherService.list());
		return "EditSucceed";
	}
	
	public String editTeachers(){
		ActionContext.getContext().put("teachers", teacherService.list());
		return "TeacherInfo";
	}

	
	public String AddTeacherA(){
		return "AddTeacher";
	}
	
	public String AddTeacherB(){
	    
		if(teacher.getTeacherID().length()!=0 && teacher.getPassword().length()!=0 && teacher.getName().length()!=0)
		{
			
				if(teacherService.find(teacher.getTeacherID())==null)
				{	
					teacher.setJoinDate(new java.sql.Date(new java.util.Date().getTime()));
					teacherService.save(teacher);
					addActionMessage("添加成功!欢迎!");
					ActionContext.getContext().put("teachers", teacherService.list());
					return "AddSucceed";
				}
				else{ 
					addActionMessage("系统检测到该教师工号之前已经注册过!请检查");
					return "AddFailed";
				}
		}

		addActionError("用户名、密码、姓名不能为空!");
		return "AddFailed";
	}

	public String DeleteTeacherA(){
		ActionContext.getContext().put("deleteTeacher", teacherService.find(teacher.getTeacherID()));
		return "DeleteTeacher";
	}
	
	public String DeleteTeacherB(){
		if(teacher!=null)
			teacherService.delete(teacher.getTeacherID());
		ActionContext.getContext().put("teachers", teacherService.list());
		return "DeleteSucceed";
	}
	
	public String Search(){
		//search by searchInfo, that is by teacherID and name;
		if(searchInfo.length()!=0){
			if(teacherService.find(searchInfo)!=null){
				//search by teacherID
				ActionContext.getContext().put("findTeacher", teacherService.find(searchInfo));
				return "SearchSucceed";
			}
			if(teacherService.findByName(searchInfo).size()!=0){
				ActionContext.getContext().put("findTeacher", teacherService.findByName(searchInfo));
				return "SearchSucceed";
			}
			ActionContext.getContext().put("teachers", teacherService.list());
			addActionMessage("没有找到该教师记录！请输入教师姓名或学号~");
			return "SearchFailed";
		}
		
		ActionContext.getContext().put("teachers", teacherService.list());
		addActionMessage("请输入教师姓名或学号");
		return 	"SearchFailed";
	}
	
	public String home(){
		ActionContext.getContext().put("courses",courseService.findByTeacherID(teacherID));
		return "home";
	}
	
	public String editTeacherSelfA(){
		ActionContext.getContext().put("editteacher", teacherService.find(teacherID));
		return "Edit";
	}
	

	public String editTeacherSelfB(){
		teacherService.update(teacher);
		addActionMessage("修改成功!欢迎!");
		ActionContext.getContext().put("courses",courseService.findByTeacherID(teacher.getTeacherID()));
		return "EditSucceed";
	}

}
